package com.hybrid.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import com.hybrid.utils.TaoLog;
import com.hybrid.webview.WVWebChromeClient;
import com.hybrid.webview.WVWebViewClient;
import com.hybrid.webview.WVWebView;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-10 21:26
 * @version: 9.1.0
 */
public class WVWebViewFragment extends Fragment {
    private static String TAG = WVWebViewFragment.class.getSimpleName();
    private WVWebView mWebView = null;
    private WVWebViewClient mWebclient = null;
    private WVWebChromeClient mChromeClient = null;
    private String url = null;
    private Activity activity;
    public static String URL = "url";

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    /** @deprecated */
    @Deprecated
    public WVWebViewFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        if(null != bundle) {
            this.url = bundle.getString(URL);
        }

    }

    @Override
    public void onDestroy() {
        if(this.mWebView != null) {
            this.mWebView.setVisibility(View.GONE);
            this.mWebView.removeAllViews();
            if(this.mWebView.getParent() != null) {
                ((ViewGroup)this.mWebView.getParent()).removeView(this.mWebView);
            }

            this.mWebView.loadUrl("about:blank");
            this.mWebView.destroy();
            this.mWebView = null;
        }

        this.activity = null;

        try {
            super.onDestroy();
        } catch (Exception var2) {
            ;
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onPause() {
        if(this.mWebView != null) {
            this.mWebView.onPause();
        }

        super.onPause();
    }

    @Override
    public void onResume() {
        if(this.mWebView != null) {
            this.mWebView.onResume();
        }

        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.getWebView();
        if(this.url != null && this.mWebView != null) {
            this.mWebView.loadUrl(this.url);
        } else {
            TaoLog.d(TAG, "image urls is null");
        }

        return this.mWebView;
    }

    public void setWebViewClient(WVWebViewClient webclient) {
        if(webclient != null) {
            this.mWebclient = webclient;
            if(this.mWebView != null) {
                this.mWebView.setWebViewClient(this.mWebclient);
            }
        }

    }

    public void setWebchormeClient(WVWebChromeClient client) {
        if(client != null) {
            this.mChromeClient = client;
            if(this.mWebView != null) {
                this.mWebView.setWebChromeClient(this.mChromeClient);
            }
        }

    }

    public WebView getWebView() {
        if(this.mWebView == null) {
            Object lActivity = this.activity == null?this.getActivity():this.activity;
            if(lActivity == null) {
                return null;
            }

            this.mWebView = new WVWebView((Context)lActivity);
            this.setWebViewClient(this.mWebclient);
            this.setWebchormeClient(this.mChromeClient);
            LayoutParams param = new LayoutParams(-1, -1);
            this.mWebView.setLayoutParams(param);
        }

        return this.mWebView;
    }

    public boolean onBackPressed() {
        if(this.getWebView() != null && this.getWebView().canGoBack()) {
            this.getWebView().goBack();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(null != this.mWebView) {
            this.mWebView.onActivityResult(requestCode, resultCode, data);
        }

    }
}
